package com.qxf.service.actions;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qxf.dao.AdpositionMgtDao;
import com.qxf.exception.UserConfigurationException;
import com.qxf.model.ReturnEvent;
import com.qxf.model.beans.AdpBean;
import com.qxf.model.beans.MediaBean;
import com.qxf.model.beans.UserSession;
import com.qxf.service.BaseAction;
import com.qxf.util.Constants;

public class AdpositionMgtAction extends BaseAction {

	private AdpositionMgtDao adDao;
	
	Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public ReturnEvent process(HttpServletRequest req, HttpServletResponse resp) {
		String op = req.getParameter("op");
		log.info("op = " + op);
		ReturnEvent responseEvent = new ReturnEvent();
		Object data = null;
		adDao = new AdpositionMgtDao();
		try {
			if("getAdList".equalsIgnoreCase(op)) {
				data = getAdList(this.getUserSession());
			} else if("addAd".equalsIgnoreCase(op)) {
				data = addAd(req, resp);
			} else if("editAd".equalsIgnoreCase(op)) {
				data = editAd(req, resp);
			} else if("generateUrl".equalsIgnoreCase(op)) {
				data = genUrl(req, resp);
			} else if("getUrl".equalsIgnoreCase(op)) {
				data = getUrl(req, resp);
			} else if("getMediaList".equalsIgnoreCase(op)) {
				data = getMediaList(req, resp);
			} else if("getAdConfiguration".equalsIgnoreCase(op)) {
				data = getAdConfiguration(req, resp);
			} else if("addMedia".equalsIgnoreCase(op)) {
				data = addMedia(req, resp);
			} else if("deleteAd".equalsIgnoreCase(op)) {
				data = deleteAd(req, resp);
			}
		} catch(UserConfigurationException uce) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			responseEvent.setErrorMessage(uce.getMessage());
			log.info("UserConfigurationException: " + uce.getMessage());
		} catch (Exception e) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			e.printStackTrace();
		}
		if(data != null) {
			responseEvent.setReturnCode(Constants.RETURN_SUCCESS);
			responseEvent.setResult(data);
		} else {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
		}
		return responseEvent;
	}
	
	/**
	 * 
	 * @param userSession
	 * @return
	 * @throws SQLException
	 * @deprecated
	 */
	private String getAdList(UserSession userSession) throws Exception {
		adDao.getAdList(userSession);
		return null;
	}
	
	private String addAd(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String mediaId = req.getParameter("mediaId");
		String adName = req.getParameter("adName");
		String activityId = userSession.getActivityInfo().getActivityId();
		if(mediaId == null || mediaId.trim().length() == 0) {
			throw new UserConfigurationException("请选择或新建媒体");
		}
		if(adName == null || adName.trim().length() == 0) {
			throw new UserConfigurationException("广告位名称不能为空");
		}
		if(adDao.addAd(mediaId, adName, activityId)) {
			return Constants.RETURN_SUCCESS;
		}
		return null;
	}
	
	private String editAd(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String adId = req.getParameter("adId");
		String adName = req.getParameter("adName");
		if(adDao.editAd(adId, adName)) {
			return Constants.RETURN_SUCCESS;
		}
		return null;
	}
	
	private String genUrl(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String adId = req.getParameter("adId");
		String serverBase = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath();
		String activityUrl = userSession.getActivityInfo().getActivityUrl();
		log.info("genUrl(): adId=" + adId + ", serverBase=" + serverBase + ", activityUrl=" + activityUrl);
		if(null != activityUrl && activityUrl.length() > 0) {
			String adUrl = adDao.generateAdUrl(serverBase, adId, activityUrl);
			if(adUrl != null && adUrl.trim().length() > 0) {
				return adUrl;
			}
		} else {
			throw new UserConfigurationException("请先设置活动主页链接");
		}
		return null;
	}
	
	private String getUrl(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String adId = req.getParameter("adId");
		log.info("getUrl(): adId=" + adId);
		if(adId != null && adId.trim().length() > 0) {
			return adDao.getAdUrl(adId);
		}
		return null;
	}
	
	private List<MediaBean> getMediaList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String userId = userSession.getUserId();
		return adDao.getMediaList(userId);
	}
	
	private AdpBean getAdConfiguration(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String userId = userSession.getUserId();
		String adId = req.getParameter("adId");
		return adDao.getAdConfiguration(userId, adId);
	}
	
	private String addMedia(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String mediaName = req.getParameter("mediaName");
		String userId = userSession.getUserId();
		if(null == mediaName || mediaName.trim().length() == 0) {
			throw new UserConfigurationException("媒体名称不能为空");
		}
		if(adDao.addMedia(mediaName, userId)) {
			return Constants.RETURN_SUCCESS;
		}
		return null;
	}
	
	private String deleteAd(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String adId = req.getParameter("adId");
		if(adDao.deleteAd(adId)) {
			return Constants.RETURN_SUCCESS;
		}
		return null;
	}

}
